package com.easytouch.core.model;


import com.easytouch.core.utils.Tools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gulin on 2018/3/16.
 */

public class WarrantyModel {
    private String mDate = ""; //生产日期yyyyMMdd
    private int mStock = 0; //数量

    public WarrantyModel() {
    }

    public WarrantyModel(final String pDate, final int pStock) {
        this.mDate = pDate;
        this.mStock = pStock;
        if (pDate != null) {
            if (pDate.length() > 6) {
                mDate = pDate.substring(2);
            }
        }
    }

    public String getDate() {
        return mDate;
    }

    public void setDate(String pDate) {
        this.mDate = pDate;
        if (pDate != null) {
            if (pDate.length() > 6) {
                mDate = pDate.substring(2);
            }
        }
    }

    public int getStock() {
        return mStock;
    }

    public void setStock(int pStock) {
        this.mStock = pStock;
    }

    public static List<WarrantyModel> string2list(final String pData) {
        List<WarrantyModel> warrantyList = new ArrayList<>();
        if (pData != null && !"".equals(pData)) {
            String[] array = pData.split(":");
            if (array != null && array.length > 0) {
                for (String item : array) {
                    if (item.length() < 8) {
                        continue;
                    }
                    String date = item.substring(0, 6);
                    String stock = item.substring(6);
                    int num = 0;
                    try {
                        num = Integer.parseInt(stock);
                    } catch (NumberFormatException e) {
                        continue;
                    }
                    if (num < 0 || num > 99) {
                        continue;
                    }
                    WarrantyModel model = new WarrantyModel(date, num);
                    warrantyList.add(model);
                }
            }
        }
        return warrantyList;
    }

    public static String list2string(final List<WarrantyModel> pList) {
        String warrantyString = "";
        if (pList != null && pList.size() > 0) {
            for (WarrantyModel model : pList) {
                String date = model.getDate();
                String stock = Tools.addLeftZero(String.valueOf(model.getStock()), 2);
                if (date.length() > 8) {
                    date = date.substring(0, 8);
                }
                if (stock.length() > 2) {
                    continue;
                }
                if (warrantyString.length() > 0) {
                    warrantyString += ":";
                }
                warrantyString += date + stock;
            }
        }
        return warrantyString;
    }
}
